package com.cupcorn.onlinescript;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

public class ObjectScoring 
{
	String name;
	String arrayValue;
	int score;
	
	public ObjectScoring(String name, ArrayList<Integer> arrayValue, int score) {
		this(name, arrayToString(arrayValue), score);
	}
	
	public ObjectScoring(String name, String array, int score)
	{
		this.name = name;
		this.arrayValue = array;
		this.score = score;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getArrayValue() {
		return arrayValue.toString();
	}
	
	public ArrayList<Integer> getArrayValue_()
	{
		ArrayList<Integer> arrays = new ArrayList<Integer>();
		String values[] = this.arrayValue.split("&");
		for(int i = 0; i < values.length; i++)
		{
			arrays.add(Integer.parseInt(values[i]));
		}
		
		return arrays;
	}

	public void setArrayValue(ArrayList<Integer> arrayValue) {
		this.arrayValue = arrayToString(arrayValue);
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
	
	public String toJsonString()
	{
		JSONObject child = new JSONObject();
		try 
		{
			child.put("array", getArrayValue());
			child.put("score", getScore());
			child.put("id", getName());
		}
		catch (JSONException e) 
		{
		    e.printStackTrace();
		}
		return child.toString();
	}
	
	static String arrayToString(ArrayList<Integer> array)
	{
		String string = "";
		for(int i = 0; i < array.size(); i++)
		{
			if(i == 0)
			{
				string += array.get(i)+"";
			}
			else
			{
				string += "&"+array.get(i);
			}
		}
		return string;
	}
}
